Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break tie for top categorical columns in Series.describe #9867

Merged
merged 4 commits into from
Dec 14, 2021

Conversation

isVoid
Copy link
Contributor

@isVoid isVoid commented Dec 8, 2021

Closes #9825

This PR fixes the bug that when there are ties with most frequent categories in categorical series, the order returned is undefined. We break the tie by sorting the category and take its top.

Also included in this PR: refactors describe to use a dictionary items to describe for better readability. And enables

@pytest.mark.xfail(reason="https://github.com/rapidsai/cudf/issues/6219")

@isVoid isVoid requested a review from a team as a code owner December 8, 2021 03:11
@github-actions github-actions bot added the Python Affects Python cuDF API. label Dec 8, 2021
@isVoid isVoid added 3 - Ready for Review Ready for review by team breaking Breaking change bug Something isn't working labels Dec 8, 2021
@galipremsagar galipremsagar added 5 - Ready to Merge Testing and reviews complete, ready to merge and removed 3 - Ready for Review Ready for review by team labels Dec 8, 2021
@galipremsagar
Copy link
Contributor

@isVoid Looks like we need to fix the style of a file

python/cudf/cudf/core/series.py Outdated Show resolved Hide resolved
@isVoid isVoid requested a review from bdice December 10, 2021 04:55
@codecov
Copy link

codecov bot commented Dec 10, 2021

Codecov Report

Merging #9867 (1136a80) into branch-22.02 (967a333) will decrease coverage by 0.06%.
The diff coverage is 5.34%.

Impacted file tree graph

@@               Coverage Diff                @@
##           branch-22.02    #9867      +/-   ##
================================================
- Coverage         10.49%   10.42%   -0.07%     
================================================
  Files               119      119              
  Lines             20305    20473     +168     
================================================
+ Hits               2130     2134       +4     
- Misses            18175    18339     +164     
Impacted Files Coverage Δ
python/cudf/cudf/__init__.py 0.00% <0.00%> (ø)
python/cudf/cudf/core/_base_index.py 0.00% <0.00%> (ø)
python/cudf/cudf/core/column/column.py 0.00% <0.00%> (ø)
python/cudf/cudf/core/column/string.py 0.00% <ø> (ø)
python/cudf/cudf/core/dataframe.py 0.00% <0.00%> (ø)
python/cudf/cudf/core/frame.py 0.00% <0.00%> (ø)
python/cudf/cudf/core/groupby/groupby.py 0.00% <0.00%> (ø)
python/cudf/cudf/core/index.py 0.00% <ø> (ø)
python/cudf/cudf/core/indexed_frame.py 0.00% <0.00%> (ø)
python/cudf/cudf/core/multiindex.py 0.00% <0.00%> (ø)
... and 18 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a72f19e...1136a80. Read the comment docs.

@galipremsagar
Copy link
Contributor

@bdice is this good to go? Looks like the review has been addressed

Copy link
Contributor

@bdice bdice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to think about return types. This isn't blocking for this PR and we can address this in a follow-up issue/PR as needed.

dtype="str",
index=index,
index=data.keys(),
nan_as_null=False,
name=self.name,
)

def _describe_timestamp(self):
Copy link
Contributor

@bdice bdice Dec 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The describe implementations appear to be casting all the values to str (aside from the numeric implementation). This does not align with Pandas behavior:

>>> s = pd.Series([
...   np.datetime64("2000-01-01"),
...   np.datetime64("2010-01-01"),
...   np.datetime64("2010-01-01"),
... ])
>>> print(type(s.describe()["top"]))
<class 'pandas._libs.tslibs.timestamps.Timestamp'>

I recognize there is an issue here with different types, namely that count and freq are not of the same type as mean, min, percentiles, or max.

Some options to resolve this (and their downsides):

  1. Current implementation: return all values as str (results are on GPU ...but data is not usable as str type).
  2. Return a pd.DataFrame or dict that can have multiple types (not a GPU DataFrame).

Personally I think option (2) is the better choice here. The summary doesn't really need to be a GPU DataFrame since it contains so few values. Do we have precedent for this kind of behavior returning a CPU (Pandas) DataFrame?

I'm content to resolve this with the current implementation as well. Just wanted to note the inconsistency. I can file an issue if needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that a copy to host that preserves more type information is probably preferable. I'm not aware of any API that returns a pandas DataFrame, but we do return pandas objects in some places. One example is DataFrame.columns, which returns a pandas Index, probably based on the same consideration of not wanting to put the data on device. Personally from an API design perspective I would prefer not to return pandas objects and would rather return something like a dict, but I can see that causing more friction for users switching between pandas and cudf.

IMO this change is out of scope for this PR in any case. We should open an issue to discuss this further and make a more holistic discussion, but we shouldn't hold up progress here.

python/cudf/cudf/core/series.py Outdated Show resolved Hide resolved
@galipremsagar
Copy link
Contributor

@gpucibot merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5 - Ready to Merge Testing and reviews complete, ready to merge breaking Breaking change bug Something isn't working Python Affects Python cuDF API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] DataFrame.describe has incorrect results for top in categorical column
4 participants